<?php
	class Agent extends MY_controller{

		function __construct(){
			parent::__construct();
		}

		function index($name = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post();
				$name = $post_data['name'] ? urldecode($post_data['name']) : 0;
			}
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('name' => $name);
			$this->load->model("Agents");
			$result = $this->Agents->get_agent_list($limit, $this->offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/agent/index/' . $name;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 4;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("action", '科目列表');
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("agent_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("agent/list.html");
		}

		function add(){
			$post_data = $this->input->post();
			if($post_data){
				if(isset($post_data['file'])) unset($post_data['file']);
				$this->db->insert("agents", $post_data);
				$insert_id = $this->db->insert_id();
				if($insert_id > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				$this->load->model("Areas");
				$province_list = $this->Areas->get_province_data();
				$this->smarty->assign("action", '添加代理商');
				$this->smarty->assign("province_list", $province_list);
				$this->smarty->display("agent/save.html");
			}
		}

		function edit($id = 0){
			$post_data = $this->input->post();
			if($post_data){
				if(isset($post_data['file'])) unset($post_data['file']);
				$this->db->where('id', $post_data['id']);
				$this->db->update('agents', $post_data);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER']);
				}else{
					show_message($_SERVER['HTTP_REFERER'], "操作失败");
				}
			}else{
				if(intval($id) > 0){
					$agent_info = get_field_by_where("agents", "*", "id='{$id}'");
					if(!$agent_info){
						show_message("/news/type_list", "数据不存在");exit;
					}
					$this->load->model("Areas");
					$province_list = $this->Areas->get_province_data();
					if($agent_info['province_code'] > 0){
						$city_list = $this->Areas->get_province_data($agent_info['province_code']);
						$this->smarty->assign("city_list", $city_list);
					}

					if($agent_info['city_code'] > 0){
						$area_list = $this->Areas->get_province_data($agent_info['city_code']);
						$this->smarty->assign("area_list", $area_list);
					}
					$this->smarty->assign("action", '添加代理商');
					$this->smarty->assign("agent_info", $agent_info);
					$this->smarty->assign("province_list", $province_list);
					$this->smarty->display("agent/save.html");
				}else{
					show_message("/agent", "非法操作");
				}
			}
		}

		function delete($ids = 0){
			if($_POST){
				$ids = $this->input->post('ids');
			}else{
				$ids = (array) $ids;
			}
			delete_data($ids, "agents");
			if($this->db->affected_rows() > 0){
				show_message($_SERVER['HTTP_REFERER'], "删除成功");
			}else{
				show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
			}
		}

		function add_school_agent(){
			$post_data = $this->input->post();
			
			if($post_data){
				$school_info = get_field_by_where("o2o_school", "*", "id='{$post_data['school_id']}'");
				if(!$school_info){
					show_message($_SERVER['HTTP_REFERER'], "网校不存在");
				}else{
					$where = "school_id='{$post_data['school_id']}'";
					$shelves_info = get_field_by_where("agents", "id", $where);
					if($shelves_info){
						echo json_encode(array("code" => 0));
					}else{
						$shelves_data['name'] = $school_info['school_name'];
						$shelves_data['school_id'] = $post_data['school_id'];
						$shelves_data['phone'] = !empty($school_info['link_tel']) ? $school_info['link_tel'] : $school_info['link_phone'];
						$shelves_data['points'] = $post_data['points'];
						$shelves_data['province_code'] = $school_info['province_code'];
						$shelves_data['province_name'] = $school_info['province_name'];
						$shelves_data['city_code'] = $school_info['city_code'];
						$shelves_data['city_name'] = $school_info['city_name'];
						$shelves_data['area_code'] = $school_info['area_code'];
						$shelves_data['area_name'] = $school_info['area_name'];
						$shelves_data['add_time'] = time();
						$this->db->insert("agents", $shelves_data);
						echo json_encode(array("code" => 1));
					}
				}
			}
		}
	}